home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-19 / iritsm3s.zip / IRITHLP.C < prev    next >
C/C++ Source or Header  |  1992-01-22  |  11KB  |  381 lines

  1. /*****************************************************************************
  2. *   Documentation processor of IRIT- a 3d solid modeller.             *
  3. ******************************************************************************
  4. * Usage:                                     *
  5. *   Irit [-t] [-l] [-h] [-o OutFileName] [-z] [InFileName]             *
  6. *                                         *
  7. * Written by:  Gershon Elber                Ver 1.0, Nov. 1991   *
  8. *****************************************************************************/
  9.  
  10. #ifdef __MSDOS__
  11. #include <stdlib.h>
  12. #endif /* __MSDOS__ */
  13.  
  14. #include <stdio.h>
  15. #include <ctype.h>
  16. #include <string.h>
  17.  
  18. #define TRUE        1
  19. #define FALSE        0
  20. #define LINE_LEN    128
  21. #define MAX_ARRAY_SIZE    128
  22.  
  23. typedef enum {
  24.     UNDEFINED_DOC_PROCESS = 0,
  25.  
  26.     PLAIN_TEXT_DOCS,
  27.     LATEX_DOCS,
  28.     IRIT_HELP_DOCS
  29. } DocProcessType;
  30.  
  31. void ProcessFiles(FILE *Fin, FILE *Fout, DocProcessType OutputDocType);
  32. void FilterLatexMods(char *Line1, char *Line2);
  33.  
  34. /*****************************************************************************
  35. *   Main module - process command line optionsd modeller.             *
  36. *****************************************************************************/
  37. void
  38. #ifdef __MSDOS__
  39. cdecl
  40. #endif /* __MSDOS__ */
  41. main(int argc, char **argv)
  42. {
  43.     FILE
  44.     *Fin = stdin,
  45.     *Fout = stdout;
  46.     DocProcessType
  47.     OutputDocType = UNDEFINED_DOC_PROCESS;
  48.  
  49.     while (argc > 1) {
  50.     if (strcmp(argv[1], "-t") == 0) {
  51.         OutputDocType = PLAIN_TEXT_DOCS;
  52.         argc--;
  53.         argv++;
  54.     }
  55.     else if (strcmp(argv[1], "-l") == 0) {
  56.         OutputDocType = LATEX_DOCS;
  57.         argc--;
  58.         argv++;
  59.     }
  60.     else if (strcmp(argv[1], "-h") == 0) {
  61.         OutputDocType = IRIT_HELP_DOCS;
  62.         argc--;
  63.         argv++;
  64.     }
  65.     else if (strcmp(argv[1], "-o") == 0) {
  66.         if ((Fout = fopen(argv[2], "w")) == NULL) {
  67.         fprintf(stderr, "Failed to open \"%s\".\n", argv[2]);
  68.         exit(1);
  69.         }
  70.         argc -= 2;
  71.         argv += 2;
  72.     }
  73.     else if (strcmp(argv[1], "-z") == 0) {
  74.         fprintf(stderr, "Usage: IritHlp [-t] [-l] [-h] [-o OutFileName] [-z] [InFileName].\n");
  75.         exit(0);
  76.     }
  77.     else
  78.         break;
  79.     }
  80.  
  81.     if (argc > 1) {
  82.     if ((Fin = fopen(argv[1], "r")) == NULL) {
  83.         fprintf(stderr, "Failed to open \"%s\".\n", argv[2]);
  84.         exit(1);
  85.     }
  86.     }
  87.  
  88.     if (OutputDocType == UNDEFINED_DOC_PROCESS) {
  89.     fprintf(stderr, "One of [-t] [-l] [-h] must be specified.\n");
  90.     exit(1);
  91.     }
  92.  
  93.     ProcessFiles(Fin, Fout, OutputDocType);
  94.  
  95.     fclose(Fin);
  96.     fclose(Fout);
  97.  
  98.     exit(0);
  99. }
  100.  
  101. /*****************************************************************************
  102. *   Reads lines from Fin and dumps them to Fout after the following         *
  103. * processing take place:                             *
  104. * Allways:                                            *
  105. *    a. Lines start with ';' are ignored ans skipped (comments).         *
  106. *    b. Lines start with '#' followed by four ingeters (indentation,         *
  107. *       # elements per line, a Boolean to specify vertical seperator, and    *
  108. *       a Boolean flag on the row (TRUE) or col (FALSE) entries order)         *
  109. *       signifies an array. All entries, one per line are part of the array  *
  110. *       until another '#' occurs. Each of the method below should do its     *
  111. *       best to create such an array.                         *
  112. * 1. OutputDocType = PLAIN_TEXT_DOCS                         *
  113. *    a. Lines start with '@' are ignored.                     *
  114. *    b. Lines start with '!' are echoed as is with underline.             *
  115. *    c. Lines start with '$' are ignored.                     *
  116. *    c. Lines start with '&' are echoed as is with underline.             *
  117. *    d. Other lines are dumped out as is after filtering out of '{\cmd * }'  *
  118. *       latex modifiers.                             *
  119. *    e. all lines are dumped out without their first character.             *
  120. * 2. OutputDocType = LATEX_DOCS                             *
  121. *    a. Lines start with '@' are echoed as is.                     *
  122. *    b. Lines start with '!' are ignored.                     *
  123. *    c. Lines start with '$' are ignored.                     *
  124. *    d. Lines start with '&' are ignored.                     *
  125. *    e. Other lines are dumped out as is.                     *
  126. *    f. all lines are dumped out without their first character.             *
  127. * 3. OutputDocType = IRIT_HELP_DOCS                         *
  128. *    a. Lines start with '@' are ignored.                     *
  129. *    b. Lines start with '!' are dumped out as is.                 *
  130. *    c. Lines start with '$' causes echoing of the '$' in a single line.     *
  131. *    d. Lines start with '&' are ignored.                     *
  132. *    e. Other lines are dumped out as is after filtering out of '{\cmd * }'  *
  133. *       latex modifiers.                             *
  134. *    f. all lines are dumped out with their first character as in input.     *
  135. *****************************************************************************/
  136. void ProcessFiles(FILE *Fin, FILE *Fout, DocProcessType OutputDocType)
  137. {
  138.     int i, j,
  139.     LineNum = 0;
  140.     char Line1[LINE_LEN], Line2[LINE_LEN], *p;
  141.  
  142.     while (fgets(Line1, LINE_LEN - 1, Fin) != NULL) {
  143.     LineNum++;
  144.     if (Line1[0] == ';') continue;
  145.  
  146.     if (Line1[0] == '#') {             /* Beginning of array definition. */
  147.         static char ArrayEntries[MAX_ARRAY_SIZE][LINE_LEN];
  148.         int i, j, k,
  149.         Indentation, NumPerLine, VertSep, NumLines, EntriesRowBased,
  150.         StartLineNum = LineNum,
  151.         NumArrayEntries = 0;
  152.  
  153.         if (sscanf(&Line1[1], "%d %d %d %d",
  154.                &Indentation, &NumPerLine,
  155.                &VertSep, &EntriesRowBased) != 4) {
  156.         fprintf(stderr, "Four integers expected in line %d\n", LineNum);
  157.         exit(1);
  158.         }
  159.  
  160.         /* Gets all the entries of the array: */
  161.         while ((p =    fgets(ArrayEntries[NumArrayEntries++],
  162.                         LINE_LEN - 1, Fin)) != NULL &&
  163.            p[0] != '#' &&
  164.            NumArrayEntries < MAX_ARRAY_SIZE) {
  165.         p += strlen(p);              /* Remove the EOL from string. */
  166.         while (*--p < ' ');
  167.         p[1] = 0;
  168.  
  169.         LineNum++;
  170.             }
  171.         NumArrayEntries--;
  172.  
  173.         if (p[0] != '#') {
  174.         fprintf(stderr,
  175.             "Array at line %d too large (max %d) or missing '#'.\n",
  176.             StartLineNum, MAX_ARRAY_SIZE);
  177.         exit(1);
  178.         }
  179.  
  180.         /* Now we have all the array elements. Computes how many lines   */
  181.         /* need and print the lines in the format this documentation is  */
  182.         /* suppose to be in.                         */
  183.         NumLines = (NumArrayEntries + NumPerLine - 1) / NumPerLine;
  184.         switch (OutputDocType) {
  185.         case PLAIN_TEXT_DOCS:
  186.             sprintf(Line2, "%%-%ds", Indentation);
  187.             fprintf(Fout, "\n");
  188.             for (i = 0; i < NumLines; i++) {
  189.             k = EntriesRowBased ? i * NumPerLine : i;
  190.             fprintf(Fout, "   ");
  191.             for (j = 0; j < NumPerLine; j++) {
  192.                 if (k >= NumArrayEntries)
  193.                 fprintf(Fout, Line2, "");
  194.                 else {
  195.                 FilterLatexMods(ArrayEntries[k],
  196.                         ArrayEntries[k]);
  197.                 fprintf(Fout, Line2, &ArrayEntries[k][1]);
  198.                 }
  199.                 k += EntriesRowBased ? 1 : NumLines;
  200.             }
  201.             fprintf(Fout, "\n");
  202.             }
  203.             fprintf(Fout, "\n");
  204.             break;
  205.         case LATEX_DOCS:
  206.             fprintf(Fout,
  207.                 "\n\n\\smallskip\n\n\\begin{center}\n    \\begin{tabular}{|");
  208.             if (VertSep)
  209.             for (i = 0; i < NumPerLine; i++)
  210.                 fprintf(Fout, "|l");
  211.             else {
  212.             fprintf(Fout, "|");
  213.             for (i = 0; i < NumPerLine; i++)
  214.                 fprintf(Fout, "l");
  215.             }
  216.             fprintf(Fout,"||} \\hline \\hline\n");
  217.  
  218.             for (i = 0; i < NumLines; i++) {
  219.             k = EntriesRowBased ? i * NumPerLine : i;
  220.             fprintf(Fout, "\t");
  221.             for (j = 0; j < NumPerLine; j++) {
  222.                 fprintf(Fout, "%s %s ",
  223.                     k < NumArrayEntries ? &ArrayEntries[k][1] : "",
  224.                     j == NumPerLine - 1 ? "\\\\\n" : "&");
  225.                 k += EntriesRowBased ? 1 : NumLines;
  226.             }
  227.             }
  228.             fprintf(Fout,
  229.                 "\t\\hline\n    \\end{tabular}\n\\end{center}\n\n\\smallskip\n\n");
  230.             break;
  231.         case IRIT_HELP_DOCS:
  232.             sprintf(Line2, "%%-%ds", Indentation);
  233.             fprintf(Fout, "\n");
  234.             for (i = 0; i < NumLines; i++) {
  235.             k = EntriesRowBased ? i * NumPerLine : i;
  236.             fprintf(Fout, "   ");
  237.             for (j = 0; j < NumPerLine; j++) {
  238.                 if (k >= NumArrayEntries)
  239.                 fprintf(Fout, Line2, "");
  240.                 else {
  241.                 FilterLatexMods(ArrayEntries[k],
  242.                         ArrayEntries[k]);
  243.                 fprintf(Fout, Line2, &ArrayEntries[k][1]);
  244.                 }
  245.                 k += EntriesRowBased ? 1 : NumLines;
  246.             }
  247.             fprintf(Fout, "\n");
  248.             }
  249.             fprintf(Fout, "\n");
  250.             break;
  251.         }
  252.  
  253.         continue;
  254.     }
  255.  
  256.     switch (OutputDocType) {
  257.         case PLAIN_TEXT_DOCS:
  258.         switch (Line1[0]) {
  259.             case '@':
  260.             case '$':
  261.             break;
  262.             case '&':
  263.             case '!':
  264.                 j = (78 - (int) strlen(&Line1[1])) / 2;
  265.                 for (i = 0; i < j; i++) fprintf(Fout, " ");
  266.             fprintf(Fout, "%s", &Line1[1]);
  267.                 for (i = 0; i < j; i++) fprintf(Fout, " ");
  268.             for (i = 1; i < (int) strlen(&Line1[1]); i++)
  269.                 fprintf(Fout, "-");
  270.             fprintf(Fout, "\n");
  271.             break;
  272.             default:
  273.             fprintf(stderr,
  274.                 "Undefine first char command at line %d.\n",
  275.                 LineNum);
  276.             /* Let it print without the first char... */
  277.             case 0:
  278.             case 10:
  279.             case 13:
  280.             case ' ':
  281.             FilterLatexMods(Line1, Line2);
  282.             fprintf(Fout, "%s", &Line2[1]);
  283.             break;
  284.         }
  285.         break;
  286.         case LATEX_DOCS:
  287.         switch (Line1[0]) {
  288.             case '!':
  289.             case '$':
  290.             case '&':
  291.             break;
  292.             case '@':
  293.             fprintf(Fout, "%s", &Line1[1]);
  294.             break;
  295.             default:
  296.             fprintf(stderr,
  297.                 "Undefine first char command at line %d.\n",
  298.                 LineNum);
  299.             /* Let it print without the first char... */
  300.             case ' ':
  301.             case 0:
  302.                 case 10:
  303.             case 13:
  304.             fprintf(Fout, "%s", Line1[1] == 0 ? "\n" : &Line1[1]);
  305.             break;
  306.         }
  307.         break;
  308.         case IRIT_HELP_DOCS:
  309.         switch (Line1[0]) {
  310.             case '@':
  311.             case '&':
  312.             break;
  313.             case '!':
  314.             fprintf(Fout, "%s",  &Line1[1]);
  315.             break;
  316.             case '$':
  317.             fprintf(Fout, "$\n");
  318.             break;
  319.             default:
  320.             fprintf(stderr,
  321.                 "Undefine first char command at line %d.\n",
  322.                 LineNum);
  323.             /* Let it print without the first char... */
  324.             case ' ':
  325.             case 0:
  326.             case 10:
  327.             case 13:
  328.             FilterLatexMods(Line1, Line2);
  329.             fprintf(Fout, "%s", &Line2[1]);
  330.             break;
  331.         }
  332.         break;
  333.     }
  334.     }
  335. }
  336.  
  337. /*****************************************************************************
  338. * Filters out expression of the form '{\cmd * }' in Line1 into '*' in line2. *
  339. * These are latex modifiers not wanted in plain text output.             *
  340. *   Also are filtered out any \? to ?, any $*$ to * and \verb+^+ to ^.       *
  341. *   A space is forced as first char in line.                     *
  342. *****************************************************************************/
  343. void FilterLatexMods(char *Line1, char *Line2)
  344. {
  345.     int i = 0,
  346.     j = 0,
  347.     InModifier = FALSE;
  348.     char Line3[LINE_LEN];
  349.  
  350.     Line3[j++] = ' ';
  351.  
  352.     while (Line1[i]) {
  353.     if (Line1[i] == '{' && Line1[i + 1] == '\\' && !isspace(Line1[i + 2])) {
  354.         /* Here is one - filter it out. */
  355.         while (!isspace(Line1[i++]));
  356.         while (isspace(Line1[i++]));
  357.         i--;
  358.         InModifier = TRUE;
  359.     }
  360.     else if (Line1[i] == '}' && InModifier) {
  361.         i++;
  362.         InModifier = FALSE;
  363.     }
  364.     else if (strncmp(&Line1[i], "\\verb+^+", 8) == 0) {
  365.         i += 8;
  366.         Line3[j++] = '^';
  367.     }
  368.     else if (Line1[i] == '\\') {
  369.         i++;
  370.     }
  371.     else if (Line1[i] == '$' && i > 0) {
  372.         i++;
  373.     }
  374.     else
  375.         Line3[j++] = Line1[i++];
  376.     }
  377.     Line3[j] = 0;
  378.     strcpy(Line2, Line3);
  379. }
  380.  
  381.